package com.liyiyan.note.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.liyiyan.note.R;
import com.liyiyan.note.bean.NoteBean;

public class DisplayAdapter extends BaseAdapter {
	private static String TAG = "DisplayAdapter";
	private List<NoteBean> notes_list;
	private LayoutInflater mInflater;
	public DisplayAdapter(Context context, List<NoteBean> notes_list){
		this.notes_list = notes_list;
		this.mInflater = LayoutInflater.from(context);
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return notes_list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		NoteBean noteMap = notes_list.get(position);

		return noteMap;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if (notes_list.get(position)==null){
			return 0;
		}
		return (long)notes_list.get(position).get_id();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		BaseViewHolder holder = null;
		if (convertView == null){
			holder = new BaseViewHolder();
			convertView = mInflater.inflate(R.layout.display_item, null);
			holder.displayTitle = (TextView)convertView
					.findViewById(R.id.display_title);
			holder.displayContent = (TextView)convertView
					.findViewById(R.id.display_content);
			convertView.setTag(holder);
			
		} else {
			holder = (BaseViewHolder)convertView.getTag();
		}
		
		NoteBean noteMap = notes_list.get(position);
		String title = noteMap.getTitleString();
		String contentString = noteMap.getContentString();
		//String date = noteMap.get("msg_date");
		if (holder==null){
			Log.i(TAG, "how can this be empty?");
			return null;
		}
		
		holder.displayTitle.setText(title);
		holder.displayContent.setText(contentString);
		
		return convertView;
	}	
	
	public void pop(int position){
		this.notes_list.remove(position);
		this.notifyDataSetChanged();
	}
	
	class BaseViewHolder {
		public TextView displayTitle;
		public TextView displayContent;
	}

}
